﻿// IWebWF
// Copyright (c) 2008
// by Michael Washington
// Webmaster@ADefWebserver.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
//
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using IWebWFCore;
using System.Web.Configuration;
using System.Configuration;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Configuration;

namespace IWebWF
{
    public partial class _Default : System.Web.UI.Page
    {
        string strCurrentVersion = "01.00.00";
        bool boolSetup = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            boolSetup = DatabaseReady();

            if (boolSetup)
            {
                lnkLogin.Visible = !User.Identity.IsAuthenticated;
                lnkLogOut.Visible = User.Identity.IsAuthenticated;
                
                // Possibly show Administration link
                if (User.Identity.IsAuthenticated && (User.Identity.AuthenticationType == "Forms"))
                {
                    FormsIdentity id = (FormsIdentity)User.Identity;
                    FormsAuthenticationTicket ticket = id.Ticket;
                    lnkAdministration.Visible = (ticket.UserData.ToString() == "Administrators");
                }
            }
            else
            {
                lnkSetup.Visible = true;
            }
        }

        #region DatabaseReady
        private bool DatabaseReady()
        {
            // This method returns true if the database exists and the table is created
            bool CanConnect = true;

            try
            {
                IWebWFDataContext IWebWFDataContext = new IWebWFDataContext();
                var result = from VersionNumber in IWebWFDataContext.Versions
                             select VersionNumber;
                CanConnect = (result.FirstOrDefault().VersionNumber == strCurrentVersion) ? true : false;
            }
            catch (Exception e)
            {
                string strError = e.Message;
                CanConnect = false;
            }

            return CanConnect;
        }
        #endregion

        #region LogOut
        protected void lnkLogOut_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            Response.Redirect("Default.aspx");
        }
        #endregion
    }
}